"use strict";
console.log("po.js loaded");
console.log(new Date().toLocaleString());
const defaultAppealReasons = [
  "我的评论内容没有任何问题，是被恶意举报脚本举报导致的误删除，请求恢复我的评论。",
  "我的评论符合平台规则，可能是被恶意举报，恳请恢复。",
  "此评论并无违规，应该是误判，希望能恢复。",
  "我的言论遵循社区规范，遭恶意举报致使评论被删，望恢复。",
  "评论内容健康且积极，无端被删，望查明并恢复。",
  "此条评论是正常交流，不存在违规，误删请恢复。",
  "我按平台规定发表评论，却被恶意处理，请求恢复。",
  "评论内容无任何不当之处，疑似误删，盼恢复。",
  "我的发言符合平台要求，被恶意举报删除，望恢复。",
  "评论内容合理合规，被错误处理，恳请恢复。",
];

//向background发送消息
chrome.runtime.sendMessage(
  {
    method: "test",
    data: "",
  },
  function (response) {
    console.log("收到来自background的回复：" + response);
  }
);
document.addEventListener("DOMContentLoaded", async function () {
  const appealReason = document.getElementById("appealReason");
  const currentCount = document.getElementById("currentCount");
  const submitAppealBtn = document.getElementById("submitAppealBtn");
  const appealLink = document.getElementById("appealLink");

  //获取当前页面视频作者信息
  chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
    const url = tabs[0].url;
    let opustype =
      url.indexOf("t.bilibili.com") !== -1 || url.indexOf("/opus") !== -1;
    console.log(appealLink, url, opustype);
    if (url.indexOf("bilibili.com/video") !== -1) {
      const bvid = url.match(/BV(\w+)/)[0];
      appealLink.value = bvid;
      chrome.tabs.sendMessage(tabs[0].id, {
        action: "GetVideoInfo",
        bvid: bvid,
      });
    } else if (opustype) {
      appealLink.value = url;
    }
  });
  await chrome.storage.sync.get("defaultAppealReason", (data) => {
    const randomIndex = Math.floor(Math.random() * defaultAppealReasons.length);
    const dfar = data.defaultAppealReason || defaultAppealReasons[randomIndex];
    appealReason.value = dfar;
    // 初始状态
    updateCharCount();
  });

  // 监听输入事件
  appealReason.addEventListener("input", updateCharCount);

  function updateCharCount() {
    const currentLength = appealReason.value.length;
    currentCount.textContent = `${currentLength}`;

    // 根据字符数启用/禁用按钮
    submitAppealBtn.disabled = currentLength < 10;
  }
});

document.getElementById("submitAppealBtn").onclick = function () {
  const appealResult = document.getElementById("appealResult");
  chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
    const url = tabs[0].url;
    console.log("url:", url);
    let opustype =
      url.indexOf("t.bilibili.com") !== -1 || url.indexOf("/opus") !== -1;
    const messagePayload = opustype
      ? {
          action: "submitAppeal",
          url: url,
          reason: document.getElementById("appealReason").value,
        }
      : {
          action: "submitAppeal",
          bvid: url.match(/BV(\w+)/)[0],
          reason: document.getElementById("appealReason").value,
        };
    //向content.js发送消息
    chrome.tabs
      .sendMessage(tabs[0].id, messagePayload)
      .then((res) => {
        console.log("收到提交申诉响应:", res);
        if (res.code === 0) {
          // 成功状态
          appealResult.className = "appeal-result success";
          appealResult.textContent = `申诉结果：${res.data.success_toast}`;
        } else if (res.code === 12082) {
          // 没有被举报
          appealResult.className = "appeal-result success";
          appealResult.textContent = `${res.message}`;
        } else {
          appealResult.className = "appeal-result error";
          appealResult.textContent = `${res.message}`;
        }
      })
      .catch((error) => {
        // 网络错误等异常情况
        appealResult.className = "appeal-result error";
        appealResult.textContent = `发生错误：${error.message}`;
      });
  });
};
